﻿using ProjScan.RecordManager.AssetData;
using ProjScan.Submodules.Scene_Check;
using ProjScan.Tools;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace ProjScan.RecordManager
{
    internal class SceneObjRecordManager : RecordManagerBase<SceneObjAssetData>
    {
        public override string TableTag => "scene";

        public override void PrepareAssets()
        {
            datas = new List<SceneObjAssetData>();
            GameObject[] array = Object.FindObjectsOfType<GameObject>();
            for (int i = 0; i < array.Length; i++)
            {
                List<string> list = new List<string>();
                int j = 0;
                Transform parent = array[i].transform.parent;
                for (; j < 4; j++)
                {
                    if (!(parent != null))
                    {
                        break;
                    }

                    list.Add(parent.name);
                    parent = parent.parent;
                }

                if (parent != null)
                {
                    list.Add("...");
                }

                SceneObjAssetData sceneObjAssetData = new SceneObjAssetData
                {
                    gameObj = array[i],
                    hierarchyList = list,
                    id = datas.Count
                };
                if (Singleton<ScanInfo>.Instance.isEditor)
                {
                    EditorUtility.DisplayProgressBar("Proj Scan - Scene Check", $"Scan: scene {Path.GetFileName(ScenesMapData.Instance.scenesList[ScenesMapData.Instance.Index])}({ScenesMapData.Instance.Index}/{ScenesMapData.Instance.scenesList.Length}))", (i + 1) / array.Length);
                }

                for (int k = 0; k < recordRules.Count; k++)
                {
                    recordRules[k].Check(sceneObjAssetData);
                }

                sceneObjAssetData.Reset();
                datas.Add(sceneObjAssetData);
            }
        }

        public override string RecordFileName()
        {
            string str = "." + ScenesMapData.Instance.Index;
            return "/table." + TableTag + str + ".csv";
        }
    }
}